package main

import (
	"os"
	"time"

	"gitee.com/mqyqingkong/submit-and-listen/client"
	"github.com/sirupsen/logrus"
)

func main() {
	var logger = &logrus.Logger{
		Out:       os.Stderr,
		Formatter: new(logrus.TextFormatter),
		Hooks:     make(logrus.LevelHooks),
		Level:     logrus.InfoLevel}

	c := client.NewClient(client.WithGetServerAddress(func() string {
		return "http://127.0.0.1:8181"
	}))
	go func() {
		//测试：异步提交任务
		for {
			params := make(map[string]string)
			params["k1"] = "v1"
			params["k2"] = "v2"
			c.Submit("/task/batch/test1", params, client.ListenerAsynFunc(func(reqId, submitUri, response string, serverError error) error {
				// 监听执行结果
				if serverError != nil {
					return serverError
				}
				logger.Infof("listener executed[test1]. reqId:%s, submitUri:%s, response:%s", reqId, submitUri, response)
				return nil
			}))
			time.Sleep(3 * time.Second)
			c.Submit("/task/batch/test1Panic", params, client.ListenerAsynFunc(func(reqId, submitUri, response string, serverError error) error {
				// 监听执行结果
				if serverError != nil {
					return serverError
				}
				logger.Infof("listener executed[test1Panic]. reqId:%s, submitUri:%s, response:%s", reqId, submitUri, response)
				return nil
			}))
			time.Sleep(3 * time.Second)
		}

	}()
	// 开始监听执行结果
	c.StartListen()
}
